//--------------------------------------
// silk-editor-plugin Project
//
// SilkColorConfigurationBlock.java
// Since: 2010/11/15
//
// $URL$ 
// $Author$
//--------------------------------------
package org.xerial.silk.editor.plugin.preferences;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.dltk.internal.ui.editor.ScriptSourceViewer;
import org.eclipse.dltk.ui.preferences.AbstractScriptEditorColoringConfigurationBlock;
import org.eclipse.dltk.ui.preferences.IPreferenceConfigurationBlock;
import org.eclipse.dltk.ui.preferences.OverlayPreferenceStore;
import org.eclipse.dltk.ui.text.IColorManager;
import org.eclipse.dltk.ui.text.ScriptSourceViewerConfiguration;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.texteditor.ITextEditor;
import org.xerial.silk.editor.plugin.text.SilkColor;
import org.xerial.silk.editor.plugin.text.SilkDocumentSetup;
import org.xerial.silk.editor.plugin.text.SilkPartitions;
import org.xerial.silk.editor.plugin.text.SimpleSilkSourceViewerConfiguration;

public class SilkSyntaxColorConfigurationBlock extends AbstractScriptEditorColoringConfigurationBlock implements
        IPreferenceConfigurationBlock
{

    private static final String PREVIEW_FILE_NAME = "testfile.silk";

    public SilkSyntaxColorConfigurationBlock(OverlayPreferenceStore store) {
        super(store);
    }

    @Override
    protected ProjectionViewer createPreviewViewer(Composite parent, IVerticalRuler verticalRuler,
            IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles, IPreferenceStore store) {
        return new ScriptSourceViewer(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles, store);
    }

    @Override
    protected ScriptSourceViewerConfiguration createSimpleSourceViewerConfiguration(IColorManager colorManager,
            IPreferenceStore preferenceStore, ITextEditor editor, boolean configureFormatter) {
        return new SimpleSilkSourceViewerConfiguration(colorManager, preferenceStore, editor,
                SilkPartitions.SILK_PARTITIONING, configureFormatter);
    }

    @Override
    protected String[][] getSyntaxColorListModel() {

        String[][] colorList = new String[SilkColor.values().length][3];
        for (SilkColor each : SilkColor.values()) {
            colorList[each.ordinal()] = new String[] { each.fieldName, each.key, sCoreCategory };
        }
        return colorList;

    }

    @Override
    protected void setDocumentPartitioning(IDocument document) {
        SilkDocumentSetup participant = new SilkDocumentSetup();
        participant.setup(document);
    }

    @Override
    protected String getPreviewContent() {
        String line;
        String separator = System.getProperty("line.separator"); //$NON-NLS-1$
        StringBuffer buffer = new StringBuffer(512);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(PREVIEW_FILE_NAME)));
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
                buffer.append(separator);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                }
                catch (IOException e) {}
            }
        }
        return buffer.toString();

    }
}
